*==============================================================================*
*===*                  Overconfidence and Gun Preferences                  *===*
*===*                * Summary statistics and regressions *                *===*
*===*                              * Figures *                             *===*
*==============================================================================*

* Authors: Fernando G. Cafferata, Patricio Dominguez, and Carlos Scartascini
* Code written by Fernando G. Cafferata and Matias Guizzo Altube
* Edited by Andres Barinas-Forero

	* ssc install catplot 
	
	* Catplot follows old syntaxis, so follow this procedure: 
	/*
	 ssc install catplot 
	 which catplot
	 ado uninstall catplot
	 copy http://fmwww.bc.edu/RePEc/bocode/c/catplot2010.ado catplot.ado
	 copy http://fmwww.bc.edu/RePEc/bocode/c/catplot2010.hlp catplot.hlp
	
	This will ensure to follow catplot 2010
	*/

****===========================================================================*
**#*===* 0. Preamble

* Clearing memory
	clear all
	clear mata
	clear matrix
	set more off
* Defining paths
	global today = strtrim(c(current_date))
	global path "C:/Users/afelipeb/OneDrive - Inter-American Development Bank Group/IADB Andres Barinas/Overconfidence_Guns/Replication Package" // This global should be changed with the personal directory in order to use the other globals (the ones are below)
	global data "$path/Data"
	global output "$path/Output"

* Choosing directory
     cd "$output/Figures"

* Setting font preferences for figures in paper/slides (Uncomment the preferred font)
	graph set window fontface "LM Roman 10" // Ideal for paper format
// 	graph set window fontface "Calibri" // Ideal for slides
	set scheme s2color


* Loading clean dataset
	use "$data/Crime_.dta", clear


****===========================================================================*
**#*===* 1. Figure 1 (Ownership and use of weapons preferences)

**#*=====* 1.1. Panel A (Agreement with carrying weapons)

*** 1.1.1. Police carrying weapons

catplot arm3 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Police", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon) ) ///
		bar(2, color(maroon*.5) ) ///
		bar(3, color(maroon*.25)) ///
		bar(4, color(gray*.1)) ///
		bar(5, color(emidblue*.25)) ///
		bar(6, color(emidblue)) ///
		bar(7, color(edkblue)) ///
	legend(rows(1) stack size(small) keygap(0) rowgap(0.25) colgap(0.25) region(lcol(none)) symxsize(*.9) ///
	order(1 "Strongly""disagree" 2 "" 3 "" 4 "" 5 "" 6 "" 7 "Strongly""agree") ///
	symplacement(center) ///
	title(Agreement, size(medsmall))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "police_pais.png", replace
		graph save police_pais.gph, replace


*** 1.1.2. Security guards carrying weapons

catplot arm4 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Security guards", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon) ) ///
		bar(2, color(maroon*.5) ) ///
		bar(3, color(maroon*.25)) ///
		bar(4, color(gray*.1)) ///
		bar(5, color(emidblue*.25)) ///
		bar(6, color(emidblue)) ///
		bar(7, color(edkblue)) ///
	legend(rows(1) stack size(small) keygap(0) rowgap(0.25) colgap(0.25) region(lcol(none)) symxsize(*.9) ///
	order(1 "Strongly""disagree" 2 "" 3 "" 4 "" 5 "" 6 "" 7 "Strongly""agree") ///
	symplacement(center) ///
	title(Agreement, size(medsmall))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "guards_pais.png", replace
		graph save guards_pais.gph, replace


*** 1.1.3. Citizens carrying weapons at home

catplot arm5 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Citizens (Home)", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon) ) ///
		bar(2, color(maroon*.5) ) ///
		bar(3, color(maroon*.25)) ///
		bar(4, color(gray*.1)) ///
		bar(5, color(emidblue*.25)) ///
		bar(6, color(emidblue)) ///
		bar(7, color(edkblue)) ///
	legend(rows(1) stack size(small) keygap(0) rowgap(0.25) colgap(0.25) region(lcol(none)) symxsize(*.9) ///
	order(1 "Strongly""disagree" 2 "" 3 "" 4 "" 5 "" 6 "" 7 "Strongly""agree") ///
	symplacement(center) ///
	title(Agreement, size(medsmall))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "civilians_home_pais.png", replace
		graph save civilians_home_pais.gph, replace


*** 1.1.4. Citizens carrying weapons on the street

catplot arm6 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Citizens (Street)", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon) ) ///
		bar(2, color(maroon*.5) ) ///
		bar(3, color(maroon*.25)) ///
		bar(4, color(gray*.1)) ///
		bar(5, color(emidblue*.25)) ///
		bar(6, color(emidblue)) ///
		bar(7, color(edkblue)) ///
	legend(rows(1) stack size(small) keygap(0) rowgap(0.25) colgap(0.25) region(lcol(none)) symxsize(*.9) ///
	order(1 "Strongly""disagree" 2 "" 3 "" 4 "" 5 "" 6 "" 7 "Strongly""agree") ///
	symplacement(center) ///
	title(Agreement, size(medsmall))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "civilians_street_pais.png", replace
		graph save civilians_street_pais.gph, replace


*** 1.1.5. All panels combined

grc1leg police_pais.gph guards_pais.gph civilians_home_pais.gph civilians_street_pais.gph, name(Fig1a, replace) graphreg(col(white)) row(1) iscale(1.3)
	graph display Fig1a, xsize(100) ysize(30)
	graph export "Fig1a.png", width(2000) height(600) replace


**#*=====* 1.2. Panel B (Offering resistance to prevent a robbery)

* Recoding missing values
	recode arm7 arm8 (5=.)
	recode arm9 (4=.)

*** 1.2.1. Resistance to a robbery on the street

catplot arm7 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Robbery in the street", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(gray*.2) ) ///
		bar(2, color(maroon*.3) ) ///
		bar(3, color(maroon*.6)) ///
		bar(4, color(maroon)) ///
	legend(rows(1) stack size(vsmall) keygap(0) rowgap(0) colgap(0.25) region(lcol(none)) ///
	order(1 "Never" 2 "Only with""a knife" 3 "Only with""a firearm" 4 "Always") ///
	symplacement(center) ///
	title(Would offer resistance, size(small))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "resist_steet_pais.png", replace
		graph save resist_steet_pais.gph, replace


*** 1.2.2. Resistance to a robbery at home (Alone)

catplot arm8 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Robbery at home, alone", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(gray*.2) ) ///
		bar(2, color(maroon*.3) ) ///
		bar(3, color(maroon*.6)) ///
		bar(4, color(maroon)) ///
	legend(rows(1) stack size(vsmall) keygap(0) rowgap(0) colgap(0.25) region(lcol(none)) ///
	order(1 "Never" 2 "Only with""a knife" 3 "Only with""a firearm" 4 "Always") ///
	symplacement(center) ///
	title(Would offer resistance, size(small))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "resist_homealone_pais.png", replace
		graph save resist_homealone_pais.gph, replace

	
*** 1.2.3. Resistance to a robbery at home (With family)

catplot arm9 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Robbery at home, with family", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(gray*.2) ) ///
		bar(2, color(maroon*.5) ) ///
		bar(3, color(maroon)) ///
	legend(rows(1) stack size(vsmall) keygap(0) rowgap(0) colgap(0.25) region(lcol(none)) ///
	order(1 "Never" 2 "Only with""equal weapon" 3 "Always") ///
	symplacement(center) ///
	title(Would offer resistance, size(small))) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "resist_homefamily_pais.png", replace
		graph save resist_homefamily_pais.gph, replace


*** 1.2.4. All sub-panels combined

graph combine resist_steet_pais.gph resist_homealone_pais.gph resist_homefamily_pais.gph, name(Fig1b, replace) graphreg(col(white)) row(1) iscale(1.3)
	graph display Fig1b, xsize(100) ysize(40)
	graph export "Fig1b.png", width(2000) height(800) replace


****===========================================================================*
**#*===* 2. Figure 2 (Overconfidence distributions)

*** 2.1. Overplacement in responsibility

catplot nmor1 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Responsibility", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon)) ///
		bar(2, color(navy)) ///
	legend(rows(1) stack size(small) keygap(0.25) rowgap(0.25) colgap(0.25) ///
	order(1 "Less" 2 "More") region(lcol(none)) symxsize(*.8) ///
	symplacement(center)) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "responsibility_pais.png", replace
		graph save responsibility_pais.gph, replace


*** 2.2. Overplacement in ability to make decisions

catplot nmor2 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Ability to make decisions", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon)) ///
		bar(2, color(gray*.3)) ///
		bar(3, color(navy)) ///
	legend(rows(1) stack size(small) keygap(0.25) rowgap(0.25) colgap(0.25) ///
	order(1 "Less" 2 "Equal" 3 "More") region(lcol(none)) symxsize(*.8) ///
	symplacement(center)) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "ability_pais.png", replace
		graph save ability_pais.gph, replace

		
*** 2.3. Overplacement in morality

catplot nmor3 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Morality", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon)) ///
		bar(2, color(gray*.3)) ///
		bar(3, color(navy)) ///
	legend(rows(1) stack size(small) keygap(0.25) rowgap(0.25) colgap(0.25) ///
	order(1 "Less" 2 "Equal" 3 "More") region(lcol(none)) symxsize(*.8) ///
	symplacement(center)) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "morale_pais.png", replace
		graph save morale_pais.gph, replace


*** 2.4. Overplacement in responsibility when using weapons (if having one)

catplot nmor4 PAIS, ///
	percent(PAIS) ///
	var1opts(label(labsize(small))) ///
	var2opts(label(labsize(small)) relabel(`r(relabel)') axis(lcol(none))) ///
	ytitle("Percent (%)", size(small) margin(small)) ///
	title("Responsibility when using wapons", size(medium) col(black)) ///
	asyvars stack ///
		bar(1, color(maroon)) ///
		bar(2, color(gray*.3)) ///
		bar(3, color(navy)) ///
	legend(rows(1) stack size(small) keygap(0.25) rowgap(0.25) colgap(0.25) ///
	order(1 "Less" 2 "Equal" 3 "More") region(lcol(none)) symxsize(*.8) ///
	symplacement(center)) ///
	graphreg(col(white)) ylab(, tlen(0) glc(gray*.1))
		graph export "weapons_pais.png", replace
		graph save weapons_pais.gph, replace

		
*** 2.5. All sub-panels combined

grc1leg responsibility_pais.gph ability_pais.gph morale_pais.gph weapons_pais.gph, name(Fig2, replace) graphreg(col(white)) row(2) iscale(.8) legend(ability_pais.gph)
	graph display Fig2, xsize(100) ysize(80)
	graph export "Fig2.png", width(2000) height(1600) replace
	
	
****===========================================================================*
**#*===* 3. Regression coefficients plotted (Determinants of overconfidence)

*** 3.1. Estimation

preserve
	label drop gender age_cat education level_income marital_status l_pais

	qui: tab age_cat,gen(ag) 
	qui: tab education, gen(ed)
	qui: tab level_income, gen(inc) 
	qui: tab marital_status, gen(mar) 
	qui: tab PAIS, gen(pa)

	reg overestimation_cat gender o.ag1 ag2 ag3 ag4 ag5 ag6 o.ed1 ed2 ed3 ed4 ed5 o.mar1 mar2 mar3 mar4 o.pa1 pa2 pa3 pa4 pa5 pa6, robust
		estimates store overestimation
	reg nmor1 gender o.ag1 ag2 ag3 ag4 ag5 ag6 o.ed1 ed2 ed3 ed4 ed5 o.mar1 mar2 mar3 mar4 o.pa1 pa2 pa3 pa4 pa5 pa6, robust
		estimates store nmor1
	reg nmor2 gender o.ag1 ag2 ag3 ag4 ag5 ag6 o.ed1 ed2 ed3 ed4 ed5 o.mar1 mar2 mar3 mar4 o.pa1 pa2 pa3 pa4 pa5 pa6, robust
		estimates store nmor2
	reg nmor3 gender o.ag1 ag2 ag3 ag4 ag5 ag6 o.ed1 ed2 ed3 ed4 ed5 o.mar1 mar2 mar3 mar4 o.pa1 pa2 pa3 pa4 pa5 pa6, robust
		estimates store nmor3
	reg nmor4 gender o.ag1 ag2 ag3 ag4 ag5 ag6 o.ed1 ed2 ed3 ed4 ed5 o.mar1 mar2 mar3 mar4 o.pa1 pa2 pa3 pa4 pa5 pa6, robust
		estimates store nmor4
restore


*** 3.2. Determinants of overestimation

coefplot overestimation, coeflabels(gender="Male" ///
		|| ag1="(ref) 18 to 29 (years)" ag2="30 to 39 (years)" ag3="40 to 49 (years)" ///
			ag4="50 to 59 (years)" ag5="60 to 69 (years)" ag6="70 or more (years)" ///
		|| ed1= "(ref) Primary or less" ed2= "Some secondary" ed3= "Secondary" ///
			ed4= "Tertiary/some college (or university)" ed5="College/univesity (and more)" ///
		|| mar1="(ref) Single" mar2="Married/civil partnership" mar3="Divorced/separated" mar4="Widow/widower" ///
		|| pa1= "(ref) Argentina" pa2= "Brazil" pa3= "Chile" pa4= "Colombia" pa5= "Mexico" pa6= "USA", notick labsize(vsmall) labgap(1) glcol(red)) ///
	headings(gender="{bf:Gender}"ag1="{bf:Age}" ed1="{bf:Education}" mar1="{bf:Marital Status}" pa1="{bf:Country}", labcolor(maroon*1.5) labsize(vsmall)) ///
	drop(_cons) omitted  ///
	title("Overestimation", size(medsmall) col(black)) ///
	xtitle("Coefficient, overestimation", size(vsmall) margin(small)) ///
	note("Overestimation in categories. Data source: LAPOP-IADB", size(tiny) span) ///
	xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
	xscale(range(-.3 .3)) ///
	xlabel(-.3 "-.3" -.15 "-.15" 0 "0" .15 ".15" .3 ".3", labsize(vsmall) tlen(0)) ///
	lwidth(thin) lpattern(dash) ///
	graphreg(col(white)) ysc(lcol(none)) col(maroon) ciop(lcol(maroon)) grid(glc(gray*.1))
		graph export "overestimation.png", replace 


*** 3.3. Determinants of overplacement (Responsibility)

coefplot nmor1, coeflabels(gender="Male" ///
		|| ag1="(ref) 18 to 29 (years)" ag2="30 to 39 (years)" ag3="40 to 49 (years)" ///
			ag4="50 to 59 (years)" ag5="60 to 69 (years)" ag6="70 or more (years)" ///
		|| ed1= "(ref) Primary or less" ed2= "Some secondary" ed3= "Secondary" ///
			ed4= "Tertiary/some college (or university)" ed5="College/univesity (and more)" ///
		|| mar1="(ref) Single" mar2="Married/civil partnership" mar3="Divorced/separated" mar4="Widow/widower" ///
		|| pa1= "(ref) Argentina" pa2= "Brazil" pa3= "Chile" pa4= "Colombia" pa5= "Mexico" pa6= "USA", notick labsize(vsmall) labgap(2)) ///
	headings(gender="{bf:Gender}"ag1="{bf:Age}" ed1="{bf:Education}" mar1="{bf:Marital Status}" pa1="{bf:Country}", labcolor(maroon*1.5) labsize(vsmall)) ///
	drop(_cons) omitted  ///
	title("Overplacement: Responsibility", size(medsmall) col(black)) ///
	xtitle("Coefficient, overplacement", size(vsmall) margin(small)) ///
	note("Overestimation in categories. Data source: LAPOP-IADB", size(tiny) span) ///
	xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
	xscale(range(-.10 .15)) ///
	xlabel(-.10 "-.10" -.05 "-.05" 0 "0" .05 ".05" .10 ".10" .15 ".15", labsize(vsmall) tlen(0)) ///
	lwidth(thin) lpattern(dash) ///
	graphreg(col(white)) ysc(lcol(none)) col(maroon) ciop(lcol(maroon)) grid(glc(gray*.1))
		graph export "responsibility.png", replace
 
 
*** 3.4. Determinants of overplacement (Decision making)
 
coefplot nmor2, coeflabels(gender="Male" ///
		|| ag1="(ref) 18 to 29 (years)" ag2="30 to 39 (years)" ag3="40 to 49 (years)" ///
			ag4="50 to 59 (years)" ag5="60 to 69 (years)" ag6="70 or more (years)" ///
		|| ed1= "(ref) Primary or less" ed2= "Some secondary" ed3= "Secondary" ///
			ed4= "Tertiary/some college (or university)" ed5="College/univesity (and more)" ///
		|| mar1="(ref) Single" mar2="Married/civil partnership" mar3="Divorced/separated" mar4="Widow/widower" ///
		|| pa1= "(ref) Argentina" pa2= "Brazil" pa3= "Chile" pa4= "Colombia" pa5= "Mexico" pa6= "USA", notick labsize(vsmall) labgap(2)) ///
	headings(gender="{bf:Gender}"ag1="{bf:Age}" ed1="{bf:Education}" mar1="{bf:Marital Status}" pa1="{bf:Country}", labcolor(maroon*1.5) labsize(vsmall)) ///
	drop(_cons) omitted  ///
	title("Overplacement: Decision making", size(medsmall) col(black)) ///
	xtitle("Coefficient, overplacement", size(vsmall) margin(small)) ///
	note("Overestimation in categories. Data source: LAPOP-IADB", size(tiny) span) ///
	xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
	xscale(range(-.10 .30)) ///
	xlabel(-.10 "-.10" 0 "0" .10 ".10" .20 ".20" .30 ".30", labsize(vsmall) tlen(0)) ///
	lwidth(thin) lpattern(dash) ///
	graphreg(col(white)) ysc(lcol(none)) col(maroon) ciop(lcol(maroon)) grid(glc(gray*.1))
		graph export "decisions.png", replace 
 

*** 3.5. Determinants of overplacement (Morality)

coefplot nmor3, coeflabels(gender="Male" ///
		|| ag1="(ref) 18 to 29 (years)" ag2="30 to 39 (years)" ag3="40 to 49 (years)" ///
			ag4="50 to 59 (years)" ag5="60 to 69 (years)" ag6="70 or more (years)" ///
		|| ed1= "(ref) Primary or less" ed2= "Some secondary" ed3= "Secondary" ///
			ed4= "Tertiary/some college (or university)" ed5="College/univesity (and more)" ///
		|| mar1="(ref) Single" mar2="Married/civil partnership" mar3="Divorced/separated" mar4="Widow/widower" ///
		|| pa1= "(ref) Argentina" pa2= "Brazil" pa3= "Chile" pa4= "Colombia" pa5= "Mexico" pa6= "USA", notick labsize(vsmall) labgap(2)) ///
	headings(gender="{bf:Gender}"ag1="{bf:Age}" ed1="{bf:Education}" mar1="{bf:Marital Status}" pa1="{bf:Country}", labcolor(maroon*1.5) labsize(vsmall)) ///
	drop(_cons) omitted  ///
	title("Overplacement: Morality", size(medsmall) col(black)) ///
	xtitle("Coefficient, overplacement", size(vsmall) margin(small)) ///
	note("Overestimation in categories. Data source: LAPOP-IADB", size(tiny) span) ///
	xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
	xscale(range(-.15 .15)) ///
	xlabel(-.20 "-.20" -.10 "-.10" 0 "0" .10 ".10" .20 ".20", labsize(vsmall) tlen(0)) ///
	lwidth(thin) lpattern(dash) ///
	graphreg(col(white)) ysc(lcol(none)) col(maroon) ciop(lcol(maroon)) grid(glc(gray*.1))
		graph export "morale.png", replace 


*** 3.6. Determinants of overplacement (Responsibility when using weapons)

coefplot nmor4, coeflabels(gender="Male" ///
		|| ag1="(ref) 18 to 29 (years)" ag2="30 to 39 (years)" ag3="40 to 49 (years)" ///
			ag4="50 to 59 (years)" ag5="60 to 69 (years)" ag6="70 or more (years)" ///
		|| ed1= "(ref) Primary or less" ed2= "Some secondary" ed3= "Secondary" ///
			ed4= "Tertiary/some college (or university)" ed5="College/univesity (and more)" ///
		|| mar1="(ref) Single" mar2="Married/civil partnership" mar3="Divorced/separated" mar4="Widow/widower" ///
		|| pa1= "(ref) Argentina" pa2= "Brazil" pa3= "Chile" pa4= "Colombia" pa5= "Mexico" pa6= "USA", notick labsize(vsmall) labgap(2)) ///
	headings(gender="{bf:Gender}"ag1="{bf:Age}" ed1="{bf:Education}" mar1="{bf:Marital Status}" pa1="{bf:Country}", labcolor(maroon*1.5) labsize(vsmall)) ///
	drop(_cons) omitted  ///
	title("Overplacement: Responsibility when using weapons", size(medsmall) col(black)) ///
	xtitle("Coefficient, overplacement", size(vsmall) margin(small)) ///
	note("Overestimation in categories. Data source: LAPOP-IADB", size(tiny) span) ///
	xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
	xscale(range(-.15 .15)) ///
	xlabel(-.3 "-.3" -.15 "-.15" 0 "0" .15 ".15" .3 ".3", labsize(vsmall) tlen(0)) ///
	lwidth(thin) lpattern(dash)  ///
	graphreg(col(white)) ysc(lcol(none)) col(maroon) ciop(lcol(maroon)) grid(glc(gray*.1))
		graph export "weapons.png", replace 

		
****===========================================================================*
**#*===* 4. Comparison with census data

* Redefining country labels

gen iso3 = "ARG" if PAIS == 1
	replace iso3 = "BRA" if PAIS == 2
	replace iso3 = "CHL" if PAIS == 3
	replace iso3 = "COL" if PAIS == 4
	replace iso3 = "MEX" if PAIS == 5
	replace iso3 = "USA" if PAIS == 6
drop country
gen country = "Argentina" if PAIS == 1
	replace country = "Brazil" if PAIS == 2
	replace country = "Chile" if PAIS == 3
	replace country = "Colombia" if PAIS == 4
	replace country = "Mexico" if PAIS == 5
	replace country = "United States" if PAIS == 6
		

*** Age and sex distributions (population vs sample)

preserve
	egen agegr = cut(Age), at(15(5)100) label

	contract agegr Sex PAIS iso3 country
	
	gen age = "15-19" if agegr == 0
		replace age = "20-24" if agegr == 1
		replace age = "25-29" if agegr == 2
		replace age = "30-34" if agegr == 3
		replace age = "35-39" if agegr == 4
		replace age = "40-44" if agegr == 5
		replace age = "45-49" if agegr == 6
		replace age = "50-54" if agegr == 7
		replace age = "55-59" if agegr == 8
		replace age = "60-64" if agegr == 9
		replace age = "65-69" if agegr == 10
		replace age = "70-74" if agegr == 11
		replace age = "75-79" if agegr == 12
		replace age = "80-84" if agegr == 13
		replace age = "85-89" if agegr == 14
		replace age = "90-94" if agegr == 15
		replace age = "95-99" if agegr == 16
	drop if agegr == .

	drop if Sex == 3
		decode Sex, gen(sex)
		replace sex = "Female" if sex == "Femenino"
		replace sex = "Male" if sex == "Masculino"

	drop PAIS agegr Sex

	bys iso3: egen sample_total = total(_freq)

	order iso3 sex age sample_total _freq
		sort iso3 age sex

	rename _freq sample_size
		gen sample_dens = sample_size/sample_total

	encode age, gen(age_enc)

	merge 1:1 iso3 sex age age_enc using "$data/WPP_sex_age"
		labmask age_enc, val(age)
		drop _merge
		replace sample_dens = 0 if sample_dens == .
		sort iso3 age_enc sex
	
	twoway (bar pop_dens age_enc, col(gray%30)) (bar sample_dens age_enc, col(maroon%30)), ///
		by(country sex, graphreg(col(white)) note("")) ///
		subti(, bcol(gray*.1)) /// 
		xlab(1/18, ang(v) value labsize(small) tlen(0)) ///
		ysc(lcol(white)) ylab(, ang(h) tlen(0) glc(gray*.1)) ///
		yti("Density", size(small)) xti("") ///
		legend(order(1 "WPP population data" 2 "Sample data") ///
			   region(lcol(white)) size(small) pos(12) symxsize(*.3) symysize(*.6)) ///
		name(WPP_compare, replace)
		graph export "Census_population.png", replace
restore


*** Education distributions (population vs sample)

preserve
	replace educ = educ - 1 if PAIS == 6 // Erase this line when correcting the data in main database
	
	contract educ iso3 country
	
	gen educ_gr = "BUPPSRY" if (educ < 11 & (iso3 == "BRA" | iso3 == "COL")) | ///
							   (educ < 12 & (iso3 != "BRA" & iso3 != "COL"))
		replace educ_gr = "UPPSRY" if educ_gr == ""
		replace educ_gr = "TRY" if (educ > 13 & (iso3 == "BRA" | iso3 == "COL")) | ///
								   (educ > 14 & (iso3 != "BRA" & iso3 != "COL"))

	gen educ_enc = 1 + (educ_gr == "UPPSRY") + 2*(educ_gr == "TRY")
		labmask educ_enc, val(educ_gr)
		la def educ_enc 1 `""Below upper" "secondary""' 2 `""Upper" "secondary""' 3 `""Tertiary" "or higher""', modify
// 	gen educ3 = 1 + (educ >= 12) + (educ >= 15) if educ < .
// 		la def educ_gr 1 "BUPPSRY" 2 "UPPSRY" 3 "TRY"
// 		la val educ3 educ_gr
// 		decode educ3, gen(educ_gr)
	
	sort iso3 educ
	rename _freq sample_size
	
	bys iso3: egen sample_total = total(sample_size)
	bys iso3 educ_gr: egen sample_educ_size = total(sample_size)

	gen sample_dens = sample_size/sample_total
	gen sample_educ_dens = sample_educ_size/sample_total
	
	merge m:m iso3 educ_gr using "$data/OECD_education", nogen

	sort iso3 educ
	
// 	twoway (bar sample_dens educ, col(maroon%30)), ///
// 		by(iso3, graphreg(col(white))) xlab(0/18, ang(v) labsize(small) tlen(0)) ///
// 		ysc(lcol(white)) ylab(0(.1).5, ang(h) tlen(0)) xti("")
	
	keep iso3 country educ_gr educ_enc pop_dens sample_educ_dens
	duplicates drop
	twoway (bar pop_dens educ_enc, col(gray%30)) (bar sample_educ_dens educ_enc, col(maroon%30)), ///
		by(country, graphreg(col(white)) note("")) ///
		subti(, bcol(gray*.1)) ///
		xlab(1/3, value labsize(small) tlen(0)) ///
		ysc(lcol(white)) ylab(, ang(h) tlen(0) glc(gray*.1)) ///
		yti("Density", size(small)) xti("") ///
		legend(order(1 "OECD population data" 2 "Sample data") ///
			   region(lcol(white)) size(small) pos(12) symxsize(*.3) symysize(*.6)) ///
		name(OECD_Educ_compare, replace)
		graph export "Census_education.png", replace
restore

*** Additional: Probability of correctly answering each question by education
// twoway (lfit OK_Ocon_1 educ) (lfit OK_Ocon_2 educ) (lfit OK_Ocon_3 educ) ///
// 	   (lfit OK_Ocon_4 educ) (lfit OK_Ocon_5 educ) (lfit OK_Ocon_6 educ) ///
// 	   (lfit OK_Ocon_7 educ) (lfit OK_Ocon_8 educ) (lfit OK_Ocon_9 educ), ///
// 	   graphreg(col(white)) ylab(, ang(h) glc(gray*.1)) xlab(0(6)18) ///
// 	   yti("Pr(Correct Answer|Education)") xti("Years of education") ///
// 	   legend(order(1 "Birthplace Rihanna" 2 "Closest planet" 3 "Minutes/Day" 4 "Astronaut moon" 5 "World Cup" 6 "Bill Gates" 7 "Highest mountain" 8 "President China" 9 "Roman year") col(3) symxsize(*.3) size(small) region(lcol(none)))
// 	   		graph export "9q-educ.png", replace